/**
 * zmn员工模块-员工渠道数据权限列表
 * 文件命名格式：zmn.模块名称.表名称.功能.js
 * @author heguanghua
 * @since 2020/06/01 19:18
 */
layui.define(['index', 'form', 'zmnApp', 'table', 'laypage'], function (exports) {
    let $ = layui.$;
    let form = layui.form;
    let layer = layui.layer;
    let table = layui.table;
    let laypage = layui.laypage;
    let zmnApp = layui.zmnApp;

    let data = { "code":0, "msg":"", "count":json.totalCount };

    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        page: true,
        limit: json.pageSize,
        height: 'full-189'
    });
    laypage.render(zmnPage.laypageOptions(json));

    // 移除渠道（批量）/添加渠道
    table.on("toolbar(zmn-table)", function (obj) {
        if(obj.event == "cancel") {
            layui.layer.confirm('确认移除选中的渠道及其下级渠道吗？', function() {
                let $ = layui.$;
                let dataIds = zmnPage.layTabldIds(obj, "dataId");
                if (dataIds == null || dataIds.length == 0) {
                    layui.layer.msg("请选择要移除的数据权限");
                    return;
                }
                zmnPage.ajaxSimple(zmnApp.SYS_BASE_URL +"/staff/dpermit/modify/channel/delete.action", {
                    staffId: json.staffId,
                    dataIds: dataIds.join(",")
                }, 1500);
            });
        } else if(obj.event == "add") {
            layer.open({
                type: 2,
                area: ['1000px', '600px'],
                fixed: false,
                maxmin: true,
                title: '选择合作',
                content: zmnApp.SYS_BASE_URL +'/common/channel/list/cooperation.action?multi=2&callbackUrl=/staff/dpermit/modify/channel/add.action&keyId=' + json.staffId
            });
        }
    });

    // 移除指定渠道
    deleteChannel = function(parentId, dataId) {
        layui.layer.confirm('确认移除该渠道吗？', function() {
            let $ = layui.$;
            let channelCount = $('.layui-table-view span[name=one_'+ parentId +']').length;
            $.ajax({
                type: "POST", url: zmnApp.SYS_BASE_URL +'/staff/dpermit/modify/channel/delete.action',
                dataType: "JSON", data: { staffId: json.staffId, dataIds: dataId },
                success:function(json) {
                    if(json.status == 200) {
                        let timeout = channelCount <= 1 ? 1500 : 0;
                        zmnPage.reload(json.message, timeout);
                        if(timeout == 0) {
                            $('.layui-table-view #two_'+ dataId).remove();
                        }
                    } else {
                        layer.msg(json.message);
                    }
                },
                error:function(XMLHttpRequest, textStatus, errorThrown){}
            });
        });
    };

    //对外暴露的接口
    exports('staff_dpermit_channel_list', {});
});